function bindElements(){

	//bind submit button
	$("#Submit, #DeclineOffer").bind('click', function(){
		if(validateElements()){
			if( this.id == 'DeclineOffer' ){
				//if decline offer is clicked, the api.cfm see's the url var and declines the offer
				$("#frmStep2").attr('action', 'api.cfm?api=update_step2_employee&Decline=1').submit();	
			}else{
				$("#frmStep2").attr('action', 'api.cfm?api=update_step2_employee').submit();
			}
			return false;
		}
	});
	
	//datepicker replacement for cfcalendar.  Flash bad.  Calendar looks bad.
	$("#EstStart_datepicker").datepicker({
		onSelect: function(dateText, inst) { $("#EstStartDate").val(dateText); }									 
	});	
	
	//Updates pay rate with data from frequency drop down
	$("#PayRate").bind('blur', function(){
		var frequency = $("#FrequencyID option:selected").val().split(':');		
		if ( $("#PayRate").val() != "" ){
			$("#AnnaulSalary").val( ($(this).val() * frequency[1]).toFixed(2) );
		}else{
			$("#AnnaulSalary").val("");
		}
	});
	
	//Updates Annaul Salary with data from frequency drop down and pay rate
	$("#AnnaulSalary").bind('blur', function(){
		var frequency = $("#FrequencyID option:selected").val().split(':');		
		if ( $("#AnnaulSalary").val() != "" ){
			$("#PayRate").val( ($(this).val() / frequency[1]).toFixed(2) );
		}else{
			$("#PayRate").val("");
		}
	});	
	
	//Updates Annaul Salary with data from frequency pay rate
	$("#FrequencyID").bind('change', function(){
		var frequency = $("#FrequencyID option:selected").val().split(':');		
		if ( $("#PayRate").val() != "" ){
			$("#AnnaulSalary").val( ($("#PayRate").val() * frequency[1]).toFixed(2) );
		}else{
			$("#AnnaulSalary").val("");
		}
	});
	
	//Scott's binding of click on Drop Down Pos
	$("#PositionID").bind('click', function(){
		$("#WorkersComp option:selected").val( $("#WorkersCompCodeID").val() );	 
	});	
	
	//loadJobInfo('#PositionID');
	
	$("#PositionID").bind('change', function(){
		var PositionInfo = getData("api.cfm?api=get_position_details&id=" + this.value);
		var WorkerComp = getData("api.cfm?api=get_workers_details&id=" + PositionInfo['WORKERSCOMPCODE']);
        $("#JobTitle").val( PositionInfo['JOBTITLE'] );
        $("#JobCode").val( PositionInfo['JOBCODE'] );
        $("#HoursPerYear").val( PositionInfo['HOURSPERYEAR'] );
        $("#PayGradeCode").val( PositionInfo['PAYGRADECODE'] );
        $("#PayGradeCodeDescription").val( PositionInfo['PAYGRADECODEDESCRIPTION'] );
        $("#SCF_UDF1").val( PositionInfo['SCF_UDF1'] );
        $("#SCF_UDF2").val( PositionInfo['SCF_UDF2'] );
		$("#SCF_UDF3").val( PositionInfo['SCF_UDF3'] );
        $("#WorkersCompCode").val( PositionInfo['WORKERSCOMPCODE'] );
        $("#WorkersCompID option[value='" + WorkerComp + "']").attr('selected', 'selected');		
	});

}

var validateElements = function(){
	//sets error count to zero, if greater then zero, some data is not valid
	var error_count = 0;

	//var for required element error message.  A custom message can 
	//be set on the input element called error_message
	var require_message = function(id){		
		return $("#" + id).attr('error_message');	
	}

	//these fields are require on form.
	var required_fields = ['First', 'Last', 'CostCode', 'PayRate', 'AnnaulSalary'];
	
	//these fields are phone number fields
	var phone_fields = ['HomePhone', 'CellPhone', 'WorkPhone'];		
	
	//these fields are integer fields
	var integer_fields = ['WorkPhoneExt', 'HoursPerWeek'];
	
	//email validation fields
	var email_fields = ['Email'];
	
	//regex validators
	var re_phone = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
	var re_email = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	//removes error messages from previous validation.
	$(".ui-state-error").removeClass();
	
	//creates html of errors
	var html_errors = function(){
		var error_link = "";		
		//if fields in the required_fields variable are empty this error is triggered.	
		$.map(required_fields,function(field){
			if( !$("#" + field).val() )	{
				error_count++;
				$("#" + field).addClass('ui-state-error');
				error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
				field + ":</span> " + require_message(field) + "<br />";
			}
		});
		
		//phone number format check, error is triggered if phone is malformed
		$.map(phone_fields, function(field){
			if( $("#" + field).val() && !$("#" + field).val().match(new RegExp(re_phone)) ){
				error_count++;
				$("#" + field).addClass('ui-state-error');
				error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
				field + ":</span> " + require_message(field) + "<br />";
			}
		});
		
		//if email field is entererd and malformed, error is triggered
		$.map(email_fields,function(field){
			if( !$("#" + field).val() || !$("#" + field).val().match(new RegExp(re_email)) ){
				error_count++;
				$("#" + field).addClass('ui-state-error');
				error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
				field + ":</span> " + require_message(field) + "<br />";
			}
		});
		
		//if ext field is entererd and malformed, error is triggered
		$.map(integer_fields,function(field){
			if( $("#" + field).val() && isNaN( $("#" + field).val() ) ){
				error_count++;
				$("#" + field).addClass('ui-state-error');
				error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
				field + ":</span> " + require_message(field) + "<br />";
			}
		});
		
		return error_link;
	};
	
	//creates jquery ui dialog box html
	$("div.entry").append('<div id="error_dialog" style="width: auto;"><p>' + html_errors() + '</p></div>');
	
	//creates jquery ui dialog box object
	var $error_dialog = $('#error_dialog').dialog({
		autoOpen: false, width: 'auto', modal: true, title: 'The following fields have errors', resizable: false,
		open: function(event, ui) { $("div.ui-dialog-titlebar").width( $(".ui-dialog").width() ); },
		close: function(event, ui) {$(this).dialog( "destroy" ) }
	});

	//if no errors in validation, well... the data is valid!
	if(error_count){
		$error_dialog.dialog('open');
		$("span[id^='error_link_']").click(function(){
			var element_id = "#" + this.id.split("_")[2];			
			$.scrollTo(element_id);//jquery pluging ScrollTo
			$error_dialog.dialog('close');
			$(element_id).focus();
		});
		return false;
	}else{return true;}
};



//ajax POST to add to database.
function submitForm(obj, api, id, successFunc, completeFunc, errorFunc){	
	var api_url = "api.cfm?api=" + api + ( (id) ? "&id=" + id : "" );
	$.ajax({
		type:'POST', url:api_url, dataType: 'json', async: true, data: obj,
		success: function() { successFunc },
		complete: function(){ completeFunc },
		error: function(){ errorFunc }
	});
}



//ajax data get
var getData = function(api, completeFunc, errorFunc){
	var data = "";	
	$.ajax({
		type:'GET', url:api, dataType: 'json', async: false,
		success: function(json){ data = json; },
		complete: function(){ completeFunc },
		error: function(){ errorFunc }
	});
	return data;
};



$(function(){
	bindElements();
});